home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtiter.cc < prev    next >
Encoding:
Text File  |  1994-09-06  |  2.4 KB  |  99 lines

  1. // CmTIter.cc
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Abstract container iterator template implementation.
  7. // -----------------------------------------------------------------
  8.  
  9.  
  10. // "int" conversion operator returns zero if at the end.
  11. //
  12. template <class T> CmTIterator<T>::operator int() const
  13. {
  14.   return !done();
  15. }
  16.  
  17.  
  18. // "++" advances the iterator to the next object and returns it.
  19. //
  20. template <class T> const T& CmTIterator<T>::operator++()
  21. {
  22.   next();
  23.   return current();
  24. }
  25.  
  26.  
  27. // "++" returns the current object and advances the iterator to the next.
  28. //
  29. template <class T> const T& CmTIterator<T>::operator++(int)
  30. {
  31.   return next();
  32. }
  33.  
  34.  
  35. // "+=" advances the iterator n places and returns the object.
  36. //
  37. template <class T> const T& CmTIterator<T>::operator+=(int num)
  38. {
  39.   for (int ii = 0; ii < num; ii++) next();
  40.   return current();
  41. }
  42.  
  43.  
  44. // "--" decrements the iterator by one and returns the object.
  45. //
  46. template <class T> const T& CmTIterator<T>::operator--()
  47. {
  48.   previous();
  49.   return current();
  50. }
  51.  
  52.  
  53. // "--" returns the current object and decrements the iterator by one.
  54. //
  55. template <class T> const T& CmTIterator<T>::operator--(int)
  56. {
  57.   return previous();
  58. }
  59.  
  60.  
  61. // "-=" decrements the iterator n places and returns the object.
  62. //
  63. template <class T> const T& CmTIterator<T>::operator-=(int num)
  64. {
  65.   for (int ii = 0; ii < num; ii++) previous();
  66.   return current();
  67. }
  68.  
  69.  
  70. // "()" returns the current iterator object.
  71. //
  72. template <class T> const T& CmTIterator<T>::operator()() const
  73. {
  74.   return current();
  75. }
  76.  
  77.  
  78. // "nextOccurrence" advances to the next occurrence of the specified item.
  79. //
  80. template <class T> const T& CmTIterator<T>::nextOccurrence(const T& rItem)
  81. {
  82.   while (!done() && !(current() == rItem)) next();
  83.   const T& out = current(); next();
  84.   while (!done() && !(current() == out)) next();
  85.   return out;
  86. }
  87.  
  88.  
  89. // "previousOccurrence" advances to the previous occurrence of the
  90. // specified item.
  91. //
  92. template <class T> const T& CmTIterator<T>::previousOccurrence(const T& rItem)
  93. {
  94.   while (!done() && !(current() == rItem)) previous();
  95.   const T& out = current(); previous();
  96.   while (!done() && !(current() == out)) previous();
  97.   return out;
  98. }
  99.